package top.lingchen.blogapi.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import top.lingchen.blogapi.bean.SysUser;
import top.lingchen.blogapi.mapper.SysUserMapper;
import top.lingchen.blogapi.service.LoginService;
import top.lingchen.blogapi.service.SysUserService;
import static top.lingchen.blogapi.vo.ErrorCode.*;

import top.lingchen.blogapi.service.TokenService;
import top.lingchen.blogapi.vo.LoginUserVo;
import top.lingchen.blogapi.vo.Result;
import top.lingchen.blogapi.vo.UserVo;

/**
 * @Author 凌宸
 * @create 2021-12-15 下午 10:30
 * @Description
 * @Version 1.0
 */
@Service
public class SysUserServiceImpl implements SysUserService {
    @Autowired
    private SysUserMapper sysUserMapper;

    @Autowired
    private TokenService tokenService;

    /**
     * 根据用户 id 查询 用户
     * @param authorId
     * @return
     */
    @Override
    public SysUser findUserById(Long authorId) {
        SysUser sysUser = sysUserMapper.selectById(authorId);
        if(sysUser == null){
            sysUser = new SysUser();
            sysUser.setNickname("凌宸");
        }
        return sysUser;
    }

    @Override
    public SysUser findUser(String account, String password) {
        LambdaQueryWrapper<SysUser> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysUser::getAccount, account);
        wrapper.eq(SysUser::getPassword, password);
        wrapper.select(SysUser::getAccount, SysUser::getId, SysUser::getAvatar, SysUser::getNickname);
        wrapper.last("limit 1");
        return sysUserMapper.selectOne(wrapper);
    }

    @Override
    public Result findUserByToken(String token) {
        /**
         * 1.token 合法性校验
         *  是否为空
         *  解析是否成功
         *  redis 是否存在
         * 2.如果失败，返回错误
         * 3.如果成功，返回对应的结果，LoginUserVo
         *
         */
        SysUser sysUser = tokenService.checkToken(token);
        if(sysUser == null){
            return Result.fail(TOKEN_ILLEGAL.getCode(), TOKEN_ILLEGAL.getMsg());
        }
        LoginUserVo loginUserVo = new LoginUserVo();
        loginUserVo.setId(String.valueOf(sysUser.getId()));
        loginUserVo.setAccount(sysUser.getAccount());
        loginUserVo.setAvatar(sysUser.getAvatar());
        loginUserVo.setNickname(sysUser.getNickname());
        return Result.success(loginUserVo);
    }

    @Override
    public SysUser findUserByAccount(String account) {
        LambdaQueryWrapper<SysUser> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysUser::getAccount, account);
        wrapper.last("limit 1");
        return sysUserMapper.selectOne(wrapper);
    }

    @Override
    public void save(SysUser sysUser) {
        // 保存用户 id 会自动生成
        // 默认生成的 id 是分布式id 采用的雪花算法
        sysUserMapper.insert(sysUser);
    }

    @Override
    public UserVo findUserVoById(Long authorId) {
        SysUser sysUser = sysUserMapper.selectById(authorId);
        if(sysUser == null){
            sysUser = new SysUser();
            sysUser.setId(1642L);
            sysUser.setNickname("凌宸");
            sysUser.setAvatar("/static/img/logo.b3a48c0.png");
        }
        UserVo userVo = new UserVo();
        BeanUtils.copyProperties(sysUser, userVo);
        userVo.setId(String.valueOf(sysUser.getId()));
        return userVo;
    }
}
